//
//  DataBase.m
//  豆瓣(阿黄)
//
//  Created by dllo on 15/11/3.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "DataBase.h"
#import <sqlite3.h>


@implementation DataBase

+ (instancetype)shareDataBase{

    static DataBase *dataBase = nil;
    if (dataBase == nil) {
        dataBase = [[DataBase alloc] init];
    }
    return dataBase;
}

static sqlite3 *db = nil;

-(void)openDataBase{

    if (db != nil) {
        NSLog(@"数据库已经打开");
        return;
    }
    NSString *sqlFile = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, 1) lastObject];
    NSString *sqlPath = [sqlFile stringByAppendingPathComponent:@"activity.sqlite"];
    NSLog(@"----%@",sqlPath);
    BOOL result = sqlite3_open(sqlPath.UTF8String, &db);
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    }else{
        
        NSLog(@"打开失败");
    }
}

-(void)createTable:(NSString *)createSql{


    BOOL result = sqlite3_exec(db, createSql.UTF8String, NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }

}

- (NSMutableArray *)selectActivity{
    
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = @"SELECT * FROM activity";
    //创建查询数据库跟随指针
    sqlite3_stmt *stmt = nil;
    //查询前的准备工作
    //参数3.限制sql语句长度,-1为不限制
    //将数据库对象db,sql语句,跟随指针对象stmt关联到一起
    BOOL result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
    }else{
        
        NSLog(@"查询失败");
    }
    //循环遍历每一行数据
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        //取出第一列数据
        int number = sqlite3_column_int(stmt, 0);
        //取出第二列数据
        const unsigned char *title = sqlite3_column_text(stmt, 1);
        //取出第三列数据
        const unsigned char *begin_time = sqlite3_column_text(stmt, 2);
        //取出第四列数据
        const unsigned char *end_time = sqlite3_column_text(stmt, 3);
        
        const unsigned char *address = sqlite3_column_text(stmt, 4);
        
        const unsigned char *category_name = sqlite3_column_text(stmt, 5);
        
        const unsigned char *wisher_count = sqlite3_column_text(stmt, 6);
        
        const unsigned char *image_hlarge = sqlite3_column_text(stmt, 7);
        
        const unsigned char *participant_count = sqlite3_column_text(stmt, 8);
        
        const unsigned char *content = sqlite3_column_text(stmt, 9);
        
        //将C语言类型转成OC类型
        NSInteger num = number;
        NSString *titleStr = [NSString stringWithUTF8String:(const char *)title];
        NSString *begin_timeStr = [NSString stringWithUTF8String:(const char *)begin_time];
        NSString *end_timeStr = [NSString stringWithUTF8String:(const char *)end_time];
        NSString *addressStr = [NSString stringWithUTF8String:(const char *)address];
        NSString *category_nameStr = [NSString stringWithUTF8String:(const char *)category_name];
        NSString *wisher_countStr = [NSString stringWithUTF8String:(const char *)wisher_count];
        NSString *image_hlargeStr = [NSString stringWithUTF8String:(const char *)image_hlarge];
        NSString *participant_countStr = [NSString stringWithUTF8String:(const char *)participant_count];
        NSString *contentStr = [NSString stringWithUTF8String:(const char *)content];
        
        
        //创建学生对象
        ActivityList *activityL = [[ActivityList alloc] init];
        activityL.number = num;
        activityL.title = titleStr;
        activityL.begin_time = begin_timeStr;
        activityL.end_time = end_timeStr;
        activityL.address = addressStr;
        activityL.category_name = category_nameStr;
        activityL.wisher_count = wisher_countStr;
        activityL.image_hlarge = image_hlargeStr;
        activityL.participant_count = participant_countStr;
        activityL.content = contentStr;
        
        [array addObject:activityL];
        
        
    }
    return array;
}

- (NSMutableArray *)selectUser:(NSInteger)key{
    
    NSMutableArray *array = [NSMutableArray array];
    NSMutableArray *array2 = [NSMutableArray array];
    
    NSString *sql = @"SELECT * FROM user";
    //创建查询数据库跟随指针
    sqlite3_stmt *stmt = nil;
    //查询前的准备工作
    //参数3.限制sql语句长度,-1为不限制
    //将数据库对象db,sql语句,跟随指针对象stmt关联到一起
    BOOL result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
    }else{
        
        NSLog(@"查询失败");
    }
    //循环遍历每一行数据
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        //取出第一列数据
        int number = sqlite3_column_int(stmt, 0);
        //取出第二列数据
        const unsigned char *name = sqlite3_column_text(stmt, 1);
        //取出第三列数据
        const unsigned char *pass = sqlite3_column_text(stmt, 2);
        
        
        //将C语言类型转成OC类型
        NSInteger num = number;
        NSString *nameStr = [NSString stringWithUTF8String:(const char *)name];
        NSString *passStr = [NSString stringWithUTF8String:(const char *)pass];
        
        
        
        //创建学生对象
        User *user = [[User alloc] init];
        user.number = num;
        user.name = nameStr;
        user.pass = passStr;
        
        [array addObject:user.name];
        [array2 addObject:user];
        
    }
    if (key == 0) {
        return array;
    }else
        return array2;
}

- (NSMutableArray *)selectMovieDetail:(NSInteger)key{
    
    NSMutableArray *array = [NSMutableArray array];
    NSMutableArray *array2 = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM movieDetail WHERE userName = '%@'", [[NSUserDefaults standardUserDefaults] objectForKey:@"name"]];
    //创建查询数据库跟随指针
    sqlite3_stmt *stmt = nil;
    //查询前的准备工作
    //参数3.限制sql语句长度,-1为不限制
    //将数据库对象db,sql语句,跟随指针对象stmt关联到一起
    BOOL result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
    }else{
        
        NSLog(@"查询失败");
    }
    //循环遍历每一行数据
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        //取出第一列数据
        int number = sqlite3_column_int(stmt, 0);
        //取出第二列数据
        const unsigned char *title = sqlite3_column_text(stmt, 1);
        //取出第三列数据
        const unsigned char *movieId = sqlite3_column_text(stmt, 2);
        
        const unsigned char *userName = sqlite3_column_text(stmt, 3);
        
        //将C语言类型转成OC类型
        NSInteger num = number;
        NSString *titleStr = [NSString stringWithUTF8String:(const char *)title];
        NSString *movieIdStr = [NSString stringWithUTF8String:(const char *)movieId];
        NSString *userNameStr = [NSString stringWithUTF8String:(const char *)userName];
        
        
        //创建学生对象
        MovieDetail *movieD = [[MovieDetail alloc] init];
        movieD.number = num;
        movieD.title = titleStr;
        movieD.movieId = movieIdStr;
        movieD.userName = userNameStr;
        
        [array addObject:movieD.title];
        [array2 addObject:movieD];
        
    }
    if (key == 0) {
        return array;
    }else
        return array2;
}


- (void)insertSql:(NSString *)sql{
    
//    NSString *sql = [NSString stringWithFormat:@"INSERT INTO activity(title, begin_time, end_time, address, category_name, wisher_count, image_hlarge, participant_count, content) VALUES('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')",activityL.title, activityL.begin_time, activityL.end_time, activityL.address, activityL.category_name, activityL.wisher_count, activityL.image_hlarge, activityL.participant_count, activityL.content];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else{
        
        NSLog(@"插入失败");
    }
    
}

-(void)dropTable:(NSString *)dropSql{
    
    BOOL result = sqlite3_exec(db, dropSql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删表成功");
    }else{
        
        NSLog(@"删表失败");
    }
}



@end
